<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head th:include="master::head">
    <title id="pageTitle">凤凰网视频抓站系统-马甲号账号管理首页</title>
</head>

<body data-type="index">
<div class="am-g tpl-g">
    <!-- 头部 -->
    <header th:include="master::header">
    </header>
    <!-- 风格切换 -->
    <div class="tpl-skiner" th:include="master::changeStyle">
    </div>
    <!-- 侧边导航栏 -->
    <div class="left-sidebar" th:include="master::sidebar">
    </div>
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper">
        <div class="row-content am-cf">
            <div class="row am-cf">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12 widget-margin-bottom-lg">
                    <div class="widget am-cf widget-body-lg">
                        <div class="widget-head am-fr">
                            <div class="widget-title am-cf">接口配置详情列表</div>
                        </div>
                        <div class="widget-body  am-fr">
                            <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                                <div class="am-form-group">
                                    <div class="am-btn-toolbar">
                                        <div class="am-btn-group am-btn-group-xs">
                                            <a>
                                            <button class="am-btn am-btn-default am-btn-success" id="api_add"
                                                    onclick="addApi()" type="button">
                                                <span class="am-icon-plus"></span> 新增
                                            </button>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
                                <div class="am-form-group tpl-table-list-select">
                                    <select id="byStatus" data-am-selected="{btnSize: 'sm'}" >
                                        <option value="2">全部</option>
                                        <option value="1">已上线</option>
                                        <option value="0">已下线</option>
                                    </select>
                                </div>
                            </div>
                            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                                <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                                    <input class="am-form-field " id="findData" type="text">
                                        <span class="am-input-group-btn">
                                            <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search" type="button" onclick="findApi()"></button>
                                        </span>
                                </div>
                            </div>
                            <div class="am-scrollable-horizontal ">
                                <table width="100%"
                                       class="am-table am-table-compact am-text-nowrap tpl-table-black am-table-hover"
                                       id="example-r">
                                    <thead>
                                    <tr>
                                        <th>App名称</th>
                                        <th>url</th>
                                        <th>栏目分类</th>
                                        <th>url请求类型</th>
                                        <th>请求头参数</th>
                                        <th>post请求参数</th>
                                        <th>上次开始抓取时间</th>
                                        <th>上次抓取成功时间</th>
                                        <th>操作</th>
                                        <th>ip协议类型</th>
                                        <th>是否启用代理ip</th>
                                        <th>代理类型</th>
                                        <th>是否启用UA</th>
                                        <th>是否启用cookie</th>
                                        <th>创建时间</th>
                                        <th>创建人</th>
                                        <th>修改时间</th>
                                        <th>修改人</th>
                                    </tr>
                                    </thead>
                                    <tbody id="spiderlist">
                                    <tr class="gradeX">
                                        <td>adcd-1234-1234-abcd</td>
                                        <td>张鹏飞</td>
                                        <td>8989089</td>
                                        <td>新加坡大数据初创公司</td>
                                        <td>2016-09-26</td>
                                        <td>2016-09-26</td>
                                        <td>西瓜视频</td>
                                        <td>搞笑</td>
                                        <td>搞笑</td>
                                        <td>32-23</td>
                                        <td>上线</td>
                                        <td>
                                            <div class="tpl-table-black-operation">
                                                <a href="javascript:;">
                                                    <i class="am-icon-hand-peace-o"></i> 上线
                                                </a>
                                                <a href="javascript:;">
                                                    <i class="am-icon-pencil"></i> 编辑
                                                </a>
                                                <a href="javascript:;" class="tpl-table-black-operation-del">
                                                    <i class="am-icon-trash"></i> 删除
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    <!-- more data -->
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <!--分页-->
                        <div class="am-u-lg-12 am-cf">
                            <div class="am-fr">
                                <div id="page">
                                </div>
                            </div>
                        </div>
                        <!--分页结束-->
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<!--    添加弹出框-->
<div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm-add">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">添加内容</div>
        <div class="am-modal-bd">
            <form class="am-form am-form-horizontal" action="/api_add" method="post" id="apiAdd">
                <div class="am-form-group">
                    <label for="sitename" class="am-u-sm-2 am-form-label">App名称</label>
                    <div class="am-u-sm-10">
                        <input type="text" id="sitename" name="sitename" placeholder="App名称">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="method" class="am-u-sm-2 am-form-label">url请求类型</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="method" name="method"  data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option value="0">Get</option>
                            <option value="1">Post</option>
                        </select>
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="url" class="am-u-sm-2 am-form-label">url</label>
                    <div class="am-u-sm-10">
                        <input type="text" id="url" name="url" placeholder="url">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="category" class="am-u-sm-2 am-form-label">栏目分类</label>
                    <div class="am-u-sm-10">
                        <input type="text" id="category" name="category" placeholder="栏目分类">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="headParam" class="am-u-sm-2 am-form-label">请求头参数</label>
                    <div class="am-u-sm-10">
                        <input type="text" id="headParam" name="headParam" placeholder="请求头参数">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="isProxy" class="am-u-sm-2 am-form-label">是否启用代理ip</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="isProxy" name="isProxy"  data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option label="0-不启用" value="0" >不启用</option>
                            <option label="1-启用" value="1">启用</option>
                        </select>
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="ipCategory" class="am-u-sm-2 am-form-label">ip协议类型</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="ipCategory" name="ipCategory" data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option value="http">http</option>
                            <option value="https">https</option>
                        </select>
                    </div>
                </div>

                <div class="am-form-group" id="anonymity" >
                    <label for="anonymous" class="am-u-sm-2 am-form-label">代理类型</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="anonymous" name="anonymous" data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option value="无">无</option>
                            <option value="高匿">高匿</option>
                            <option value="匿名">匿名</option>
                            <option value="透明">透明</option>
                        </select>
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="param" class="am-u-sm-2 am-form-label">post请求参数</label>
                    <div class="am-u-sm-10">
                        <input type="text" id="param" name="param" placeholder="请求参数">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="isUA" class="am-u-sm-2 am-form-label">是否启用UA</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="isUA"  name="isUA" data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option value="0" >不启用</option>
                            <option value="1">启用</option>
                        </select>
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="isCookie" class="am-u-sm-2 am-form-label">是否启用Cookie</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="isCookie" name="isCookie" data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option value="0" >不启用</option>
                            <option value="1">启用</option>
                        </select>
                    </div>
                </div>
            </form>
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn" data-am-modal-cancel >取消</span>
            <span class="am-modal-btn" data-am-modal-confirm>确定</span>
        </div>
    </div>
</div>

<!--  修改弹框   -->
<div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">修改内容</div>
        <div class="am-modal-bd">
            <form class="am-form am-form-horizontal" action="/api_edit" method="post" id="apiEdit">
                <input type="hidden" id="guid2" name="guid">
                <div class="am-form-group">
                    <label for="sitename" class="am-u-sm-2 am-form-label">App名称</label>
                    <div class="am-u-sm-10">
                        <input type="text" id="sitename2" name="sitename" placeholder="App名称">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="method" class="am-u-sm-2 am-form-label">url请求类型</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="method2" name="method" placeholder="" data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option selected value=""></option>
                            <option value="0">Get</option>
                            <option value="1">Post</option>
                        </select>
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="url" class="am-u-sm-2 am-form-label">url</label>
                    <div class="am-u-sm-10">
                        <input type="text" id="url2" name="url" placeholder="url">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="category" class="am-u-sm-2 am-form-label">栏目分类</label>
                    <div class="am-u-sm-10">
                        <input type="text" id="category2" name="category" placeholder="栏目分类">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="headParam" class="am-u-sm-2 am-form-label">请求头参数</label>
                    <div class="am-u-sm-10">
                        <input type="text" id="headParam2" name="headParam" placeholder="请求头参数">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="isProxy" class="am-u-sm-2 am-form-label">是否启用代理ip</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select  id="isProxy2" name="isProxy" placeholder="" data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option selected value=""></option>
                            <option value="0" >不启用</option>
                            <option value="1">启用</option>
                        </select>
                    </div>
                </div>

                <div class="am-form-group" >
                    <label for="ipCategory" class="am-u-sm-2 am-form-label">ip协议类型</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="ipCategory2" name="ipCategory" placeholder="" data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option selected value=""></option>
                            <option value="http">http</option>
                            <option value="https">https</option>
                        </select>
                    </div>
                </div>

                <div class="am-form-group"  id="anonymity2" >
                    <label for="anonymous" class="am-u-sm-2 am-form-label">代理类型</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="anonymous2" name="anonymous" placeholder="" data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option selected value=""></option>
                            <option value="无">无</option>
                            <option value="高匿">高匿</option>
                            <option value="匿名">匿名</option>
                            <option value="透明">透明</option>
                        </select>
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="param" class="am-u-sm-2 am-form-label">post请求参数</label>
                    <div class="am-u-sm-10">
                        <input type="text" id="param2" name="param" placeholder="请求参数">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="isUA" class="am-u-sm-2 am-form-label">是否启用UA</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="isUA2" name="isUA" placeholder="" data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option selected value=""></option>
                            <option value="0" >不启用</option>
                            <option value="1">启用</option>
                        </select>
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="isCookie" class="am-u-sm-2 am-form-label">是否启用Cookie</label>
                    <div class="am-u-sm-10 am-form-label ">
                        <select id="isCookie2" name="isCookie" placeholder="" data-am-selected="{btnWidth: '100%', btnSize: 'sm', btnStyle: 'am-btn am-btn-default'}" >
                            <option selected value=""></option>
                            <option value="0" >不启用</option>
                            <option value="1">启用</option>
                        </select>
                    </div>
                </div>
            </form>
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn" data-am-modal-cancel >取消</span>
            <span class="am-modal-btn" data-am-modal-confirm>确定</span>
        </div>
    </div>
</div>
<!--弹出框-->
<div th:include="master::footercommon">
</div>
<link rel="stylesheet" type="text/css" href="assets/css/amazeui.page.css">
<script type="text/javascript" src="assets/js/amazeui.page.js"></script>
<script th:inline="javascript">

    $(function () {
        loadData();
        // $("#isProxy").click(function(){
        //     var flag=$("#isProxy option:checked").val();
        //     alert(flag);
        //     if (flag=="1"||flag==1){
        //
        //         $("#anonymity").removeAttr("hidden");
        //         $("#anonymity2").removeAttr("hidden");
        //     } else{
        //
        //         $("#anonymity").attr("hidden","hidden");
        //         $("#anonymity2").attr("hidden","hidden");
        //     }
        // })
    });

    function findApi() {
        var sitename=$('#findData').val();
        var status=$('#byStatus option:selected').val();
        $("#spiderlist tr").remove();
        $.ajax({
            url:"/api/findListBySite",
            type:'POST',
            cache:false,
            processData:false,
            dataType:"json",
            contentType:'application/json;charset=UTF-8',
            data:JSON.stringify({
                sitename:sitename,
                status:status
            }),
            success:function (data) {
                if (data.code == 200) {
                    appendData(data.data);
                }
                if (data.code ==500){
                    alert(500+"获取数据失败！");
                }
            },
            error:function () {
                alert("获取数据失败！");
            }
        });
    }

    function loadData() {
        //返回的是一个page示例，拥有实例方法
        var total = [[${session.apitotal}]];
        var pageSize = 30;
        var $page = $("#page").page({
            pages: Math.ceil(total / pageSize), //页数
            curr: 1, //当前页
            type: 'default', //主题
            groups: 5, //连续显示分页数
            prev: '<', //若不显示，设置false即可
            next: '>', //若不显示，设置false即可
            first: "首页",
            last: "尾页", //false则不显示
            /*
             * 触发分页后的回调，如果首次加载时后端已处理好分页数据则需要在after中判断终止或在jump中判断first是否为假
             */
            jump: function (context, first) {
                console.log('当前第：' + context.option.curr + "页");
                //ajax获取数据
                $("#spiderlist tr").remove();
                $.ajax({
                    url: "/api/getCacheDataByPage",
                    type: 'POST',
                    cache: false,
                    data: JSON.stringify({
                        page: context.option.curr,   //把页码发送到后台
                        pageSize: pageSize
                    }),
                    processData: false,
                    contentType: 'application/json;charset=UTF-8',
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 200) {
                            appendData(data.data);
                        }
                    },
                    error: function () {
                        alert("获取数据失败！");
                    }
                });
            }
        });
    }


    function appendData(datas) {
        var str = '';
        for (var i = 0; i < datas.length; i++) {
            var data = datas[i];
            str += '<tr class="gradeX">';
            str += '    <td id="guid" hidden="hidden">' + getUndefined(data.guid) + '</td>';
            str += '    <td id="sitename">' + getUndefined(data.sitename) + '</td>';
            str += '    <td>' + overUrl(getUndefined(data.url)) + '</td>';
            str += '    <td class="am-hide" id="url">' + getUndefined(data.url) + '</td>';
            str += '    <td id="category">' + getUndefined(data.category) + '</td>';
            str += '    <td id="method">' + getMethodName(data.method) + '</td>';
            str += '    <td>' + overUrl(getUndefined(data.headParam)) + '</td>';
            str += '    <td class="am-hide" id="headParam">' + getUndefined(data.headParam) + '</td>';
            str += '    <td>' + overUrl(getUndefined(data.param)) + '</td>';
            str += '    <td class="am-hide" id="param">' + getUndefined(data.param) + '</td>';
            str += '    <td id="lastSpiderTime">' + getUndefined(data.lastSpiderTime) + '</td>';
            str += '    <td id="lastSpiderSuccessTime">' + getUndefined(data.lastSpiderSuccessTime) + '</td>';
            str += '    <td>';
            str += '        <div class="tpl-table-black-operation">';
            if (data.status == 1) {
                str += '<span>      <a class="switch" data-status="1" data-guid="' + getUndefined(data.guid) + '">';
                str += '                <i class="am-icon-check"></i> <span>已上线</span>';
                str += '            </a>';
                str += '</span>';
            } else {
                str += '<span>      <a class="switch tpl-table-black-operation-del" data-status="0" data-guid="' + getUndefined(data.guid) + '">';
                str += '                <i class="am-icon-close"></i> <span>已下线</span>';
                str += '            </a>';
                str += '</span>';
            }
            str += '            <a class="api_edit">';
            str += '                <i class="am-icon-pencil"></i> 编辑';
            str += '            </a>';
            str += '            <a class="tpl-table-black-operation-del" href="javascript:if(confirm(\'确实要删除该内容吗?\'))location=\'/api_del?guid=' + getUndefined(data.guid) + '\'">';
            str += '                <i class="am-icon-trash"></i> 删除';
            str += '            </a>';
            str += '        </div>';
            str += '    </td>';
            str += '    <td id="ipCategory">' + getUndefined(data.ipCategory) + '</td>';
            str += '    <td id="isProxy">' + getMethod2Name(data.isProxy) + '</td>';
            str += '    <td id="anonymous">' + getUndefined(data.anonymous) + '</td>';
            str += '    <td id="isUA">' + getMethod2Name(data.isUA) + '</td>';
            str += '    <td id="isCookie">' + getMethod2Name(data.isCookie) + '</td>';
            str += '    <td id="createTime">' + getUndefined(data.createTime) + '</td>';
            str += '    <td id="creator">' + getUndefined(data.creator) + '</td>';
            str += '    <td id="updateTime">' + getUndefined(data.updateTime) + '</td>';
            str += '    <td id="updater">' + getUndefined(data.updater) + '</td>';
            str += '</tr>';
        }
        $("#spiderlist").html(str);
        //绑定
            bindClick();
            modalClick();
    }

    function bindClick() {
        $('.switch').click(function () {
            console.log($(this).attr('data-status'));
            console.log($(this).attr('data-guid'));
            var status = $(this).attr('data-status');
            var guid = $(this).attr('data-guid');
            switchClick($(this), status, guid);

        })

    }
    function modalClick() {
        $('.api_edit').click(function () {
            var sitename = $(this).parents("td").siblings("td#sitename").text();
            var url = $(this).parents("td").siblings("td#url").html();
            var method = $(this).parents("td").siblings("td#method").html();
            var category = $(this).parents("td").siblings("td#category").html();
            var headParam = $(this).parents("td").siblings("td#headParam").html();
            var param = $(this).parents("td").siblings("td#param").html();
            var anonymous = $(this).parents("td").siblings("td#anonymous").html();
            var isProxy = $(this).parents("td").siblings("td#isProxy").html();
            var isUA = $(this).parents("td").siblings("td#isUA").html();
            var isCookie = $(this).parents("td").siblings("td#isCookie").html();
            var ipCategory = $(this).parents("td").siblings("td#ipCategory").html();
            var guid =$(this).parents("td").siblings("td#guid").html();
            console.log(sitename+" "+ method +" "+ url +" "+ guid);
            $("#my-confirm #guid2").val(guid);
            $("#my-confirm #sitename2").val(sitename);
            $("#my-confirm #category2").val(category);
            $("#my-confirm #method2").val(method);
            $("#my-confirm #url2").val(url);
            $("#my-confirm #headParam2").val(headParam);
            $("#my-confirm #param2").val(param);
            $("#my-confirm #isProxy2").val(isProxy);
            $("#my-confirm #isUA2").val(isUA);
            $("#my-confirm #isCookie2").val(isCookie);
            $("#my-confirm #ipCategory2").val(ipCategory);
            $("#my-confirm #anonymous2").val(anonymous);
            //得到信息，显示到弹出框上
            $('#my-confirm').modal({
                relatedTarget: this,
                onConfirm: function (options) {
                    $("#apiEdit").submit();
                }
            });
        });
    }
    
    function changeDisplay() {
        var flag=$("#isProxy option:checked").val();
        alert(flag);
        if (flag=="1"||flag==1){

            $("#anonymity").removeAttr("hidden");
            $("#anonymity2").removeAttr("hidden");
        } else{

            $("#anonymity").attr("hidden","hidden");
            $("#anonymity2").attr("hidden","hidden");
        }
    }


    function getUndefined(data) {
        if (typeof(data) == "undefined" || data == "null" || !data) {
            return "";
        }
        return data;
    }

    function overUrl(url) {
        if (url.length > 30) {
            url = url.substring(0, 30) + "...";
        }
        return url;
    }

    function getMethodName(data){
        if(getUndefined(data)==0){
            return "Get";
        }
        if (getUndefined(data)==1){
            return "Post";
        }
        return "";
    }
    function getMethod2Name(data){
        if(getUndefined(data)==0){
            return "不";
        }
        if (getUndefined(data)==1){
            return "启用";
        }
        return "";
    }


    function switchClick(_this, status, guid) {
        appendSwitch(_this, status);
        $.ajax({
            url: "/change_api_status",
            type: 'POST',
            cache: false,
            data: JSON.stringify({
                "status": status,
                "guid": guid
            }),
            processData: false,
            contentType: 'application/json;charset=UTF-8',
            dataType: "json",
            success: function (data) {
                if (data.code == 200) {
                    appendSwitch(_this, status);
                }
            },
            error: function () {
                alert("状态操作失败！");
            }
        });
    }

    function appendSwitch(_this, status) {
        if (status ==  1) {
            _this.attr("data-status", "0");
            _this.attr("class", "switch tpl-table-black-operation-del");
            _this.children('i').attr("class", "am-icon-close tpl-table-black-operation-del");
            _this.children('span').text("已下线");

        } else {
            _this.attr("data-status", "1");
            _this.attr("class", "switch");
            _this.children('i').attr("class", "am-icon-check");
            _this.children('span').text("已上线");

        }
    }

    function addApi() {
        $('#my-confirm-add').modal({
            relatedTarget: this,
            onConfirm: function (options) {
                $("#apiAdd").submit();
            }
        });
    }
</script>
</body>

</html>